package cshi.thread;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

//TIJ-v4 654
class LiftOff implements Runnable {
	protected int countDown = 10;
	private static int taskCount = 0;
	private final int id = taskCount++;//任务id，每创建一次，static变量自增一次
	
	
	private static int numPerLine = 0;//各线程都在使用它，应该加以保护
	public LiftOff() {
		// TODO Auto-generated constructor stub
	}
	public LiftOff(int count) {
		this.countDown = count;
	}
	public String status() {
		return "#" + id + "(" + (countDown > 0 ? countDown : "liftoff)") + "), ";
	}
	public void run() {
		while (countDown-- > 0){
			System.out.print(status());
			numPerLine++;
			if (numPerLine == 10){
				System.out.println();
				numPerLine = 0;
			}
			Thread.yield();
		}
	}
}

class TaskWithResult implements Callable<String>{
	private int id;
	public TaskWithResult(int id) {
		this.id = id;
	}
	
	public String call(){
		return "result " + id;
	}
}

class AnonymousThread {
	private Thread thread;
	private int cnt = 5;
	public AnonymousThread(String name) {
		//内部线程类
		thread = new Thread(name) {
			@Override
			public void run() {
				while (true){
					if (--cnt == 0)
						return;
					System.out.println("thread:" + Thread.currentThread().getName());
				}
			}
		};
		
		thread.start();
	}
}

public class Executor {
	public static void basicThread() {
		Thread thread = new Thread(new LiftOff());
		
		thread.start();
		System.out.println("wait for liftoff");
	}
	
	public static void moreBasicThread() {
		//创建多个任务，static变量保证任务id不一样
		for (int i=0; i<5; i++){
			new Thread(new LiftOff()).start();
		}
	}
	
	//newSingleThreadExecutor不需要在共享资源上保持同步
	public static void singleThreadExec() {
		//会将提交的任务进行排队
		ExecutorService es = Executors.newSingleThreadExecutor();
		for (int i=0; i<5; i++){
			es.execute(new LiftOff());
		}
		es.shutdown();//如果没关闭会影响程序的退出
	}
	public static void cachedThreadPool() {
		ExecutorService eService = Executors.newCachedThreadPool();
		for (int i=0; i<5; i++){
			eService.execute(new LiftOff());
		}
		eService.shutdown();//如果没关闭会影响程序的退出
	}
	
	public static void callableDemo() {
		ExecutorService es = Executors.newCachedThreadPool();
		ArrayList<Future<String>> results = new ArrayList<Future<String>>();
		
		for (int i=10; i>0; i--){
			results.add(es.submit(new TaskWithResult(i)));
		}
		
		//迭代获取每个callable执行后的结果,get会阻塞直到完成
		for (Future<String> fs:results){
			try {
				System.out.println(fs.get());
			} catch (Exception e) {
				System.out.println(e);
			}finally {
				es.shutdown();
			}
		}
	}
	public static void main(String[] args) {
		System.out.println("cur thread:" + Thread.currentThread().getName());
		//new AnonymousThread("cs");
		//callableDemo();
		//singleThreadExec();
		//cachedThreadPool();
		//basicThread();
		moreBasicThread();
		
	}

}
